package com.dhp.campus.contoller;


import com.dhp.campus.Common.ErrorCode;
import com.dhp.campus.Common.Result;
import com.dhp.campus.Common.ResultUtils;
import com.dhp.campus.Exception.exception;
import com.dhp.campus.domain.request.addFriendRequest;
import com.dhp.campus.domain.vo.FriendsRecordsVO;
import com.dhp.campus.service.FriendsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/friends")
public class FriendsController {

    @Autowired
    private FriendsService friendsService;

    @GetMapping("/getRecords")
    public Result<List<FriendsRecordsVO>> getRecords(HttpServletRequest request){
        List<FriendsRecordsVO> friendsList = friendsService.getRecords(request);
        return ResultUtils.success(friendsList);
    }

    /**
     * 添加好友
     * @param friendRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> addFriends(@RequestBody addFriendRequest friendRequest, HttpServletRequest request){
        Long userId = friendRequest.getUserId();
        if (userId == null){
            throw new exception(ErrorCode.BUSINESS_ERROR , "用户id为空");
        }

        Boolean res = friendsService.addFriends(userId , request);
        return ResultUtils.success(res);
    }


    @GetMapping("/getMyRecords")
    public Result<List<FriendsRecordsVO>> getMyRecord(HttpServletRequest request){
        List<FriendsRecordsVO> myApply = friendsService.getMyRecord(request);
        return ResultUtils.success(myApply);
    }

    @PostMapping("/agree/{id}")
    public Result<Boolean> agreeApply(@PathVariable Long id , HttpServletRequest request){
        if (id == null){
            throw new exception(ErrorCode.PARAMS_EMPTY);
        }
        Boolean res = friendsService.agreeApply(id , request);
        return ResultUtils.success(res);
    }

    @PostMapping("/refuse/{id}")
    public Result<Boolean> reguseApply(@PathVariable Long id , HttpServletRequest request){
        if (id == null){
            throw new exception(ErrorCode.PARAMS_EMPTY);
        }
        Boolean res = friendsService.refuseApply(id , request);
        return ResultUtils.success(res);
    }


    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteFriend(@PathVariable Long id , HttpServletRequest request){
        if (id == null){
            throw new exception(ErrorCode.PARAMS_EMPTY);
        }
        Boolean res = friendsService.deleteFriend(id , request);
        return ResultUtils.success(res);
    }


}
